{% extends "master.html" %}

{% block title %}{{ repo.name }}{% endblock %}
{% set tag = "home" %}


{% block content %}
<div class="repo-header p-t-1">
  <div class="container">
    <header>
        <h2 class="repo-name m-b-0">
            <a href="{{ url_for('view_repo', username=username, repo=repo.name)}}">
              {% if repo.is_fork -%}
                <span class="oi" data-glyph="fork"></span>
                {{ repo.user.user }}/{{ repo.name}}
              {%- else -%}
                {{ repo.name }}
              {%- endif -%}
            </a>
            {% if authenticated %}
            <div class="pull-xs-right">
              {% if repo.settings.get('issue_tracker', True)
                    and config.get('ENABLE_TICKETS', True)%}
              <a href="{{ url_for('new_issue', username=username, repo=repo.name) }}"
                class="btn btn-success btn-sm">New Issue</a>
              {% endif %}
              <div class="btn-group">
              {% if not repo.is_fork %}
                <form method="POST" name="fork_project" id="fork_project"
                  action="{{ url_for(
                    '.fork_project', repo=repo.name, username=username) }}">
                  {{ forkbuttonform.csrf_token }}
                </form>
                <button class="btn btn-sm btn-secondary"
                  onclick="$('#fork_project').submit();">
                  <span class="oi" data-glyph="fork"></span> Fork</button>
              {% endif %}
                <button title="{%- if is_watching(repo.name, repo.user.user if repo.is_fork else None)
                %}You are watching this project{% else %}
                You are not watching this project{% endif -%}"
                type="button" class="btn btn-sm dropdown-toggle
                {%- if is_watching(repo.name, repo.user.user if repo.is_fork else None)
                %} btn-primary{% else %} btn-secondary{% endif -%}"
                data-toggle="dropdown" id="watch-button">
                  <span class="oi" data-glyph="eye"></span>
                </button>
                <div class="dropdown-menu dropdown-menu-right watch-menu">
                  <form method="POST" id="watch_project" name="watch_project"
                      action="{{ url_for('.watch_repo', user=repo.user.user if repo.is_fork else None,
                                         repo=repo.name, watch=0) }}">
                   {%- if is_watching(repo.name, repo.user.user if repo.is_fork else None)
                   %}
                   <a class="dropdown-item" id="unwatch_button">Stop watching {{repo.name}}</a>
                   {% else %}
                   <a class="dropdown-item" id="watch_button">Watch {{repo.name}}</a>
                   {% endif -%}

                    {{ forkbuttonform.csrf_token }}
                  </form>
                </div>
              </div>
            </div>
            {% endif %}

          {% if authenticated %}
            <div class="btn-group pull-xs-right">
            </div>
          {% endif %}
        </h2>
        {% if repo.is_fork %}
        <div class="m-t-0">
            Forked from
            {% if repo.parent.is_fork %}
            <a href="{{ url_for('view_repo', repo=repo.parent.name, username=repo.parent.user.user)}}">
            {% else %}
            <a href="{{ url_for('view_repo', repo=repo.parent.name)}}">
            {% endif %}
                {{ repo.parent.fullname }}
            </a>
        </div>
        {% endif %}
        <div class="projectinfo m-t-1 m-b-1">
        {% if repo.description %}{{ repo.description | safe }}{% else %}-{% endif -%}
        {%- if repo.url %} &nbsp;| &nbsp;<a class="inline" href="{{ repo.url }}">{{ repo.url }}</a>{% endif %}
        </div>
    </header>
    <ul class="nav nav-tabs nav-small">
      <li class="nav-item">
        <a {%
          if select == 'overview' %}class="nav-link active" {%
          else %}class="nav-link" {%
          endif %}href="{{ url_for('view_repo',username=username, repo=repo.name) }}">
            <span class="oi hidden-md-up" data-glyph="home"></span>
            <span class="hidden-sm-down">Overview</span>
        </a>
      </li>
        {% if config['DOC_APP_URL'] and repo and repo.settings.get('project_documentation', True) %}
        <li class="nav-item" >
          <a  {%
            if select == 'docs' %}class="active nav-link" {%
            else %}class="nav-link" {%
            endif %}href="{{ url_for('.view_docs', username=username, repo=repo.name) }}">
              <span class="oi hidden-md-up" data-glyph="book"></span>
              <span class="hidden-sm-down">Docs</span>
          </a>
        </li>
        {% endif %}

        <li class="nav-item{%
            if repo_obj and repo_obj.is_empty %} disabled{% endif %}">
          <a {%
            if select == 'commits' %}class="active nav-link" {%
            else %}class="nav-link" {%
            endif %}href="{{ url_for('.view_commits', username=username,
                     repo=repo.name, branchname=branchname) }}">
              <span class="oi hidden-md-up" data-glyph="spreadsheet">
              </span><span class="hidden-sm-down">Commits</span>
            </a>
        </li>

        <li class="nav-item{%
            if repo_obj and repo_obj.is_empty %} disabled{% endif %}">
          <a {%
            if select == 'tree' %}class="active nav-link" {%
            else %}class="nav-link"{%
            endif %} href="{{ url_for('.view_tree', username=username,
                     repo=repo.name, identifier=branchname) }}">
              <span class="oi hidden-md-up" data-glyph="file"></span>
              <span class="hidden-sm-down">Files</span>
          </a>
        </li>

        <li class="nav-item">
          <a {%
            if select == 'tags' %}class="active nav-link" {%
            else %}class="nav-link" {%
            endif %}href="{{ url_for('.view_tags', username=username,
                     repo=repo.name) }}">
              <span class="oi hidden-md-up" data-glyph="box"></span>
              <span class="hidden-sm-down">Releases</span>
          </a>
        </li>

        {% if config.get('ENABLE_TICKETS', True) and repo
              and repo.settings.get('issue_tracker', True) %}
        <li class="nav-item">
            <a {%
              if select == 'issues' %}class="active nav-link" {%
              else %}class="nav-link" {%
              endif %}href="{{ url_for('.view_issues', username=username,
                       repo=repo.name) }}">
                <span class="oi hidden-md-up" data-glyph="warning"></span>
                <span class="hidden-sm-down">Issues&nbsp;</span>
                <span class="label label-default label-pill hidden-sm-down">
                  {{ repo.open_tickets if repo_admin else repo.open_tickets_public }}
                </span>
            </a>
        </li>
        {% endif %}

        {% if repo and repo.settings.get('pull_requests', True) %}
        <li class="nav-item">
          <a {%
            if select == 'requests' %}class="active nav-link" {%
            else %}class="nav-link" {%
            endif %}href="{{ url_for('.request_pulls', username=username,
                     repo=repo.name) }}">
              <span class="oi hidden-md-up" data-glyph="task"></span>
              <span class="hidden-sm-down">Pull Requests&nbsp;</span>
              <span class="label label-default label-pill hidden-sm-down">
                {{ repo.open_requests }}
              </span>
          </a>
        </li>
        {% endif %}

        {% if repo and repo.forks %}
        <li class="nav-item">
          <a {%
            if select == 'forks' %}class="active nav-link" {%
            else %}class="nav-link" {%
            endif %}href="{{ url_for('.view_forks', username=username,
                     repo=repo.name) }}">
              <span class="oi hidden-md-up" data-glyph="fork"></span>
              <span class="hidden-sm-down">Forks&nbsp;</span>
              <span class="hidden-sm-down label label-default label-pill">
                {{ repo.forks |length }}
              </span>
            </a>
        </li>
        {% endif %}

      {% if authenticated %}
        {% if repo_admin %}
          <li class="nav-item pull-xs-right">
            <a title="Settings" {%
              if select == 'settings' %}class="active nav-link" {%
              else %}class="nav-link" {%
              endif %}href="{{ url_for(
                '.view_settings', repo=repo.name, username=username) }}">
                <span class="oi" data-glyph="cog"></span>
            </a>
          </li>
        {% endif %}
      {% endif %}
      {% if endpoint == 'view_docs' %}
      <li class="nav-item pull-xs-right">
        <a class="nav-link" href="{{ config['DOC_APP_URL'] + url_for('.view_repo',
                username=username, repo=repo.name, filename=filename)
          }}" target="_blank" title="Open to a new tab">
          <span class="oi" data-glyph="external-link"></span>
        </a>
      </li>
      {% endif %}

    </ul>
  </div>
</div>

<div class="container p-t-3">
{% block repo %}
{% endblock %}
</div>
{% endblock %}

{% block jscripts %}
{{ super() }}
<script type="text/javascript">
$(document).ready(function() {
  $(".watch-menu a").click(function(){
    var selectedValue = $(this).attr('id');
    var action = $("#watch_project").attr('action');
      if (selectedValue != "unwatch_button") {
        action = action.replace('/settings/0', '/settings/1');
        $('#watch_project').attr('action', action);
      }
      $('#watch_project').submit();

  });
});
</script>
{% endblock %}
